package isv

import (
    //"strconv"
    "dhfshop/models/isv"
	"dhfshop/controllers/base"
	//"github.com/astaxie/beego"
	"github.com/astaxie/beego/validation"
	"wxsdk/miniapp/wxapp"
	"encoding/base64"
)

type IsvCodeController struct {
	base.BaseController
}

// 为授权的小程序帐号上传小程序代码
func (this *IsvCodeController) Commit() {
    v := struct {
	    AuthorizerAppid  string  `form:"authorizer_appid" valid:"Required"`
		TemplateId       int     `form:"template_id" valid:"Required"`
		ExtJson          string  `form:"ext_json" valid:"Required"`
		UserVersion      string  `form:"user_version" valid:"Required"`
		UserDesc         string  `form:"user_desc" valid:"Required"`	
	}{} 
	this.ParseForm(&v)
	valid := validation.Validation{}
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return 
	}
	custAccount, err := isv.GetCustAccountByAppId(v.AuthorizerAppid)
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	code, err := wxapp.CommitCode(custAccount.AccessToken,v.TemplateId,v.ExtJson,v.UserVersion,v.UserDesc)
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	rsp := struct {
		Code   int     `json:"code"`
	    Msg    string  `json:"msg"`
	}{Code:code, Msg:""}
	this.Data["json"] = rsp
	this.ServeJSON()
}

// 获取体验小程序的体验二维码
func (this *IsvCodeController) QrCodeGet() {
    v := struct {
	    AuthorizerAppid  string  `form:"authorizer_appid" valid:"Required"`
	}{} 
	this.ParseForm(&v)
	valid := validation.Validation{}
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return 
	}
	custAccount, err := isv.GetCustAccountByAppId(v.AuthorizerAppid)
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	data, _, err := wxapp.GetQrcode(custAccount.AccessToken)
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	encodeString := base64.StdEncoding.EncodeToString(data)
	this.Ctx.ResponseWriter.Write([]byte(encodeString))
}
// 获取授权小程序帐号的可选类目
func (this *IsvCodeController) CategoryGet() {
    v := struct {
	    AuthorizerAppid  string  `form:"authorizer_appid" valid:"Required"`
	}{} 
	this.ParseForm(&v)
	valid := validation.Validation{}
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return 
	}
	custAccount, err := isv.GetCustAccountByAppId(v.AuthorizerAppid)
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	data, code, err := wxapp.GetCategory(custAccount.AccessToken)
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	rsp := struct {
		Code   int     `json:"code"`
	    Data   string  `json:"data"`
	}{Code:code, Data:data}
	this.Data["json"] = rsp
	this.ServeJSON()
}

//将第三方提交的代码包提交审核
func (this *IsvCodeController) SubmitAudit() {
    v := struct {
	    AuthorizerAppid  string  `form:"authorizer_appid" valid:"Required"`
		Data             string  `form:"data" valid:"Required"`
	}{} 
	this.ParseForm(&v)
	valid := validation.Validation{}
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return 
	}
	custAccount, err := isv.GetCustAccountByAppId(v.AuthorizerAppid)
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	auditId, code, err := wxapp.SubmitAudit(custAccount.AccessToken, v.Data)
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	rsp := struct {
		Code      int   `json:"code"`
	    AuditId   int   `json:"auditid"`
	}{Code:code, AuditId:auditId}
	this.Data["json"] = rsp
	this.ServeJSON()
}

//获取第三方提交的审核版本的审核状态
func (this *IsvCodeController) AuditStatusGet() {
    v := struct {
	    AuthorizerAppid string `form:"authorizer_appid" valid:"Required"`
		AuditId         int    `form:"audit_id" valid:"Required"`
	}{} 
	this.ParseForm(&v)
	valid := validation.Validation{}
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return 
	}
	custAccount, err := isv.GetCustAccountByAppId(v.AuthorizerAppid)  
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	status, code, err := wxapp.GetAuditStatus(custAccount.AccessToken, v.AuditId)
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	rsp := struct {
		Code      int   `json:"code"`
	    Status    int   `json:"status"`
	}{Code:code, Status:status}
	this.Data["json"] = rsp
	this.ServeJSON()
}

//发布已通过审核的小程序
func (this *IsvCodeController) ReleaseAdd() {
    v := struct {
	    AuthorizerAppid string `form:"authorizer_appid" valid:"Required"`
	}{} 
	this.ParseForm(&v)
	valid := validation.Validation{}
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return 
	}
	custAccount, err := isv.GetCustAccountByAppId(v.AuthorizerAppid)  
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	code, err := wxapp.ReleaseCode(custAccount.AccessToken)
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	rsp := struct {
		Code  int      `json:"code"`
	    Msg   string   `json:"msg"`
	}{Code:code, Msg:""}
	this.Data["json"] = rsp
	this.ServeJSON()
}

//修改小程序线上代码的可见状态
func (this *IsvCodeController) ChangeVisitStatus() {
    v := struct {
	    AuthorizerAppid string `form:"authorizer_appid" valid:"Required"`
	    Action          string `form:"action" valid:"Required"`
	}{} 
	this.ParseForm(&v)
	valid := validation.Validation{}
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return 
	}
	custAccount, err := isv.GetCustAccountByAppId(v.AuthorizerAppid)  
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	code, err := wxapp.ChangeVisitStatus(custAccount.AccessToken, v.Action)
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	rsp := struct {
		Code  int      `json:"code"`
	    Msg   string   `json:"msg"`
	}{Code:code, Msg:""}
	this.Data["json"] = rsp
	this.ServeJSON()
}

//获取小程序的第三方提交代码的页面配置
func (this *IsvCodeController) PageGet() {
    v := struct {
	    AuthorizerAppid  string  `form:"authorizer_appid" valid:"Required"`
	}{} 
	this.ParseForm(&v)
	valid := validation.Validation{}
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return 
	}
	custAccount, err := isv.GetCustAccountByAppId(v.AuthorizerAppid)
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	data, code, err := wxapp.GetPage(custAccount.AccessToken)
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	rsp := struct {
		Code   int     `json:"code"`
	    Data   string  `json:"data"`
	}{Code:code, Data:data}
	this.Data["json"] = rsp
	this.ServeJSON()
}



